package com.cskaoyan.market.controller;

import com.cskaoyan.market.bo.OrderDetailBo;
import com.cskaoyan.market.db.domain.MarketOrder;
import com.cskaoyan.market.service.MarketOrderService;
import com.cskaoyan.market.service.MarketOrderServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.cskaoyan.market.vo.Express;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin/order/*")
public class AdminOrderController extends HttpServlet {
    MarketOrderService orderService = new MarketOrderServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/order/", "");
        if ("list".equals(op)) {
            list(req, resp);
        }
        if ("detail".equals(op)) {
            detail(req, resp);
        }
        if("channel".equals(op)) {
            channel(req,resp);
        }
    }


    private void channel(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<Express> expressesList= orderService.channel();
        Object o = ResponseUtil.ok(expressesList);
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(o));
    }

    private void detail(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String idInput = req.getParameter("id");
        int id=0;
        try {
            id=Integer.parseInt(idInput);
        }catch (NumberFormatException e){
            System.out.println("输入格式错误");
        }
        OrderDetailBo orderDetailBo = orderService.detail(id);
        Object o = ResponseUtil.ok(orderDetailBo);
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(o));
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String pageInput = req.getParameter("page");
        String limitInput = req.getParameter("limit");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        String orderStatusArrayInput = req.getParameter("orderStatusArray");
        String startInput = req.getParameter("start");
        String endInput = req.getParameter("end");
        String userIdInput = req.getParameter("userId");

        List<MarketOrder> marketOrderList = orderService.list(pageInput, limitInput, sort, order, orderStatusArrayInput, startInput, endInput, userIdInput);
        Object o = ResponseUtil.okList(marketOrderList);
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(o));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/order/", "");
        if ("delete".equals(op)) {
            delete(req, resp);
        }if("refund".equals(op)) {
            refund(req,resp);
        }if("ship".equals(op)){
            shipping(req,resp);
        }if("reply".equals(op)){
            replay(req,resp);
        }

    }

    private void replay(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = req.getReader().readLine();
        String commentId = JacksonUtil.parseString(requestBody, "commentId");
        String content = JacksonUtil.parseString(requestBody, "content");
        try{
            int commentIdInput = Integer.parseInt(commentId);
            boolean reply = orderService.reply(commentIdInput, content);
            if(reply){
                Object o = ResponseUtil.ok();
                resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(o));
            }else {
                Object o = ResponseUtil.fail(620, "回复失败");
                resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(o));
            }
        }catch (IOException e) {
            Object o = ResponseUtil.fail(620, "回复失败");
            resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(o));
        }

    }

    private void shipping(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        /*{"orderId":19,"shipChannel":"ZTO","shipSn":"2022071609858911"}*/
        String requestBody = req.getReader().readLine();
        String id = JacksonUtil.parseString(requestBody,"orderId");
        String shipChannel = JacksonUtil.parseString(requestBody, "shipChannel");
        String shipSn = JacksonUtil.parseString(requestBody,"shipSn");
        int orderId = 0;
        try {
            orderId = Integer.parseInt(id);
         Boolean status= orderService.ship(orderId,shipChannel,shipSn);
            if(status) {
                Object o = ResponseUtil.ok();
                resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(o));
            }else {
                Object o = ResponseUtil.fail(620, "订单未付款");
                resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(o));
            }
        }catch (NumberFormatException e) {
            System.out.println("输入格式错误");
        }
    }

    private void refund(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        /*{"orderId":19,"refundMoney":395}*/
        String requestBody = req.getReader().readLine();
        String id = JacksonUtil.parseString(requestBody, "orderId");
        String money = JacksonUtil.parseString(requestBody, "refundMoney");
        try {
            int orderId = Integer.parseInt(id);
            int refundMoney = Integer.parseInt(money);
            Boolean refund = orderService.refund(orderId, refundMoney);
            if(refund) {
                Object o = ResponseUtil.ok();
                resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(o));
            }else {
                Object o = ResponseUtil.fail(620, "订单不能确认收货");
                resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(o));
            }
        }catch (Exception e) {
            System.out.println("输入格式错误");
        }
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        String requestBody = req.getReader().readLine();
        String id = JacksonUtil.parseString(requestBody, "orderId");

        int orderId = 0;
        try {
            orderId = Integer.parseInt(id);
            System.out.println("orderId = " + orderId);
            Object status=null;
            Boolean delete = orderService.delete(orderId);
            if(delete) {
                status = ResponseUtil.ok();
            }else {
                status = ResponseUtil.fail(623, "订单不能删除");
            }
            resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(status));
        } catch (NumberFormatException e) {
            System.out.println("输入格式错误");
        }

    }
}
